class_name GoodsButton
extends ColorRect
@onready var button: Button = $BuyButton
@onready var texture_rect: TextureRect = $TextureRect
@onready var label: Label = $Label

var item_id:String

var new_id:String



func _ready() -> void:
	var characters = 'abcdefghijklmnopqrstuvwxyz'
	new_id = generate_id(characters,10)

	button.pressed.connect(_goods_pressed)
	GameEvents.bought.connect(_bought_sign)
	
	label.text = item_id



func _goods_pressed():
	GameEvents.buy_it.emit(item_id,new_id)
	
	

func _bought_sign(new_id_context:String):
	if new_id_context == new_id:
		texture_rect.visible = false
		button.visible = false
		label.visible = false


func generate_id(chars,length):
	var word :String
	var n_char = len(chars)
	
	for i in range(length):
		word += chars[randi()%n_char]
	
	return word
	
